/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package comunicacao;

import entities.Teatro;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;

/**
 *
 * @author Leonardo Lino
 */
public class RmiClient {

    ReceiveMessageInterface rmiServer;
    Registry registry;
    String serverAddress = "127.0.0.1";
    String serverPort = "3232";
    String text;
    
    
    public ArrayList<String> getLista(){
        
        ArrayList<String> lista = null;
        
        try {
            registry = LocateRegistry.getRegistry(
                    serverAddress,
                    (new Integer(serverPort)).intValue());
            System.out.println("meu registo:" + registry.list());

            // look up the remote object
            rmiServer =(ReceiveMessageInterface) (registry.lookup("AppServer"));
            
            // call the remote method
           lista = rmiServer.listTheaters();
            

        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (NotBoundException e) {
            e.printStackTrace();
        }
        return lista;
        
    }
    
    public Teatro getTeatro(String teatroNome){
        
        Teatro teatro = null;
        try {
            registry = LocateRegistry.getRegistry(
                    serverAddress,
                    (new Integer(serverPort)).intValue());
            System.out.println("meu registo:" + registry.list());

            // look up the remote object
            rmiServer =(ReceiveMessageInterface) (registry.lookup("AppServer"));
            
            // call the remote method
           teatro = rmiServer.getTheater(teatroNome);
            

        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (NotBoundException e) {
            e.printStackTrace();
        }
        return teatro;
    }

    public String start() {

        try {
            registry = LocateRegistry.getRegistry(
                    serverAddress,
                    (new Integer(serverPort)).intValue());
            System.out.println("meu registo:" + registry.list());

            // look up the remote object
            rmiServer =(ReceiveMessageInterface) (registry.lookup("dbServer"));
            
            // call the remote method
            Teatro teatro = rmiServer.getTheater("teatro12");
            return teatro.nome;

        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (NotBoundException e) {
            e.printStackTrace();
        }
        
        return "nada";
    }
}
